import { writeBLEState } from '@/public/BluetoothFun'
import { toast } from '@/utils'

// 检查阻抗数据是否异常
export const checkImpedanceAbnormal = (impedanceL, impedanceR) => {
  return impedanceL > 1000 || impedanceR > 1000
}

// 处理监测数据
export const handleMonitoringData = async (patientId, monitorData) => {
  try {
    const { impedanceL, impedanceR, impedanceAll } = monitorData
    
    // 准备更新的数据
    const updateData = {
      leftBrainImpedance: impedanceL,
      rightBrainImpedance: impedanceR,
      allBrainImpedance: impedanceAll,
    }

    // 停止监测
    await writeBLEState(99)

    // 更新患者数据
    const success = await updateJsonById(
      patientId,
      updateData,
      '/sdcard/test/patient.json'
    )

    if (success) {
      toast('数据更新成功')
      return true
    } else {
      toast('数据更新失败')
      return false
    }
  } catch (error) {
    console.error('处理监测数据时发生错误:', error)
    toast('处理数据时发生错误')
    return false
  }
}

// 图表配置生成器
export const generateChartOptions = (xAxisData = [], seriesData = []) => {
  return {
    grid: {
      top: '0%',
      left: '5%',
      right: '5%',
      bottom: '15%',
      containLabel: true,
      show: false,
      backgroundColor: 'transparent',
      borderColor: 'transparent',
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: xAxisData,
      axisLine: { show: false },
      axisTick: { show: false },
      axisLabel: { show: false },
      splitLine: {
        show: true,
        lineStyle: {
          color: 'rgba(255, 255, 255, 0.1)',
          type: 'solid',
          width: 1,
        },
      },
    },
    yAxis: {
      type: 'value',
      axisLine: { show: false },
      axisTick: { show: false },
      axisLabel: { show: false },
      splitLine: {
        show: true,
        lineStyle: {
          color: 'rgba(255, 255, 255, 0.1)',
          type: 'solid',
          width: 1,
        },
      },
    },
    series: [
      {
        data: seriesData,
        type: 'line',
        lineStyle: {
          color: '#6DF7BE',
          width: 2,
        },
        symbol: 'none',
        smooth: true,
      },
    ],
    backgroundColor: 'transparent',
  }
} 